/*
	Veh implements the service for the veh functionality
*/
package vehService

import (
//	"strings"

	"github.com/ifq/veh-mng/models/vehModels"
	"github.com/ifq/veh-mng/services"
	"github.com/ifq/veh-mng/utilities/helper"
	"github.com/ifq/veh-mng/utilities/mongo"
	"github.com/ifq/tracelog"
	"github.com/kelseyhightower/envconfig"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

//** TYPES

type (
	// vehConfiguration contains settings for running the veh service
	vehConfiguration struct {
		Database string
	}
)

//** PACKAGE VARIABLES

var Config vehConfiguration

//** INIT

func init() {
	// Pull in the configuration
	err := envconfig.Process("veh", &Config)
	if err != nil {
		tracelog.COMPLETED_ERROR(err, helper.MAIN_GO_ROUTINE, "Init")
	}
}

//** PUBLIC FUNCTIONS

// // FindStation retrieves the specified station
// func FindStation(service *services.Service, stationId string) (vehStation *vehModels.VehStation, err error) {
// 	defer helper.CatchPanic(&err, service.UserId, "FindStation")

// 	tracelog.STARTED(service.UserId, "FindStation")

// 	queryMap := bson.M{"station_id": stationId}

// 	vehStation = &vehModels.VehStation{}
// 	err = service.DBAction(Config.Database, "veh_stations",
// 		func(collection *mgo.Collection) error {
// 			tracelog.TRACE(service.UserId, "FindStation", "Query : %s", mongo.ToString(queryMap))
// 			return collection.Find(queryMap).One(vehStation)
// 		})

// 	if err != nil {
// 		if strings.Contains(err.Error(), "not found") == false {
// 			tracelog.COMPLETED_ERROR(err, service.UserId, "FindStation")
// 			return vehStation, err
// 		}

// 		err = nil
// 	}

// 	tracelog.COMPLETED(service.UserId, "FindStation")
// 	return vehStation, err
// }

// ListVehicles retrieves the stations for the specified region
func ListVehicles(service *services.Service) (vehicles []*vehModels.VehicleInfo, err error) {
	defer helper.CatchPanic(&err, service.UserId, "ListVehicles")

	tracelog.STARTED(service.UserId, "ListVehicles")

	queryMap := bson.M{}

	vehicles = []*vehModels.VehicleInfo{}
	err = service.DBAction(Config.Database, "vehicles",
		func(collection *mgo.Collection) error {
			tracelog.TRACE(service.UserId, "ListVehicles", "Query : %s", mongo.ToString(queryMap))
			return collection.Find(queryMap).All(&vehicles)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "ListVehicles")
		return vehicles, err
	}

	tracelog.COMPLETED(service.UserId, "ListVehicles")
	tracelog.INFO(service.UserId, "ListVehicles", "get %d records.\n", len(vehicles))
	return vehicles, err
}


func FindVehicle(service *services.Service, id string) (vehicle *vehModels.VehicleInfo, err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindVehicle")

	tracelog.STARTED(service.UserId, "FindVehicle")

	//queryMap := bson.M{"_id" : id}

	vehicle = &vehModels.VehicleInfo{}
	err = service.DBAction(Config.Database, "vehicles",
		func(collection *mgo.Collection) error {
			//tracelog.TRACE(service.UserId, "FindVehicle", "Query : %s", mongo.ToString(queryMap))
			return collection.FindId(bson.ObjectIdHex(id)).One(&vehicle)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "FindVehicle")
		return vehicle, err
	}

	tracelog.COMPLETED(service.UserId, "FindVehicle")
	tracelog.INFO(service.UserId, "FindVehicle", "get records.%v\n", vehicle)
	return vehicle, err
}

func DeleteVehicle(service *services.Service, id string) (vehicle *vehModels.VehicleInfo, err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindVehicle")

	tracelog.STARTED(service.UserId, "FindVehicle")

	queryMap := bson.M{"VehicleId" : id}

	vehicle = &vehModels.VehicleInfo{}
	err = service.DBAction(Config.Database, "vehicles",
		func(collection *mgo.Collection) error {
			tracelog.TRACE(service.UserId, "FindVehicle", "Query : %s", mongo.ToString(queryMap))
			return collection.Find(queryMap).One(&vehicle)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "FindVehicle")
		return vehicle, err
	}

	tracelog.COMPLETED(service.UserId, "FindVehicle")
	tracelog.INFO(service.UserId, "FindVehicle", "get records.%v\n", vehicle)
	return vehicle, err
}


func UpdateVehichle(service *services.Service, vehicle *vehModels.VehicleInfo) (err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindVehicle")

	tracelog.STARTED(service.UserId, "FindVehicle")

	//queryMap := bson.M{"VehicleId" : id}

	//vehicle = &vehModels.VehicleInfo{}
	err = service.DBAction(Config.Database, "vehicles",
		func(collection *mgo.Collection) error {
			tracelog.TRACE(service.UserId, "FindVehicle", "Query : %s", vehicle.VehicleId)
			return collection.UpdateId(vehicle.ID, vehicle)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "FindVehicle")
		return err
	}

	tracelog.COMPLETED(service.UserId, "FindVehicle")
	tracelog.INFO(service.UserId, "FindVehicle", "get records.%v\n", vehicle)
	return err
}


func NewVehichle(service *services.Service, vehicle *vehModels.VehicleInfo) (err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindVehicle")

	tracelog.STARTED(service.UserId, "FindVehicle")

	//queryMap := bson.M{"VehicleId" : id}

	//vehicle = &vehModels.VehicleInfo{}
	err = service.DBAction(Config.Database, "vehicles",
		func(collection *mgo.Collection) error {
			tracelog.TRACE(service.UserId, "FindVehicle", "Query : %s", vehicle.VehicleId)
			return collection.Insert(vehicle)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "FindVehicle")
		return err
	}

	tracelog.COMPLETED(service.UserId, "FindVehicle")
	tracelog.INFO(service.UserId, "FindVehicle", "get records.%v\n", vehicle)
	return err
}










